package com.xgy.userauthsystem.dto;

import lombok.Getter;
import lombok.Setter;

/**
 * 统一API响应格式
 * 用于规范前后端数据交互格式
 * @param <T> 数据类型
 */
@Setter
@Getter
public class ApiResponse<T> {

    private boolean success;// 响应是否成功
    private String message;// 响应消息
    private T data;// 响应数据
    private String errorCode;// 错误码
    private long timestamp;// 响应时间戳

    // 构造方法
    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResponse(boolean success, String message, T data) {
        this();// 调用无参构造方法，初始化时间戳
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public ApiResponse(boolean success, String message, String errorCode) {
        this();
        this.success = success;
        this.message = message;
        this.errorCode = errorCode;
    }

    // 成功响应静态方法
    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(true, message, data);
    }

    public static <T> ApiResponse<T> success(String message) {
        return new ApiResponse<>(true, message, null);
    }

    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(true, "操作成功", data);
    }

    // 失败响应静态方法
    public static <T> ApiResponse<T> error(String message, String errorCode, T data) {
        ApiResponse<T> response = new ApiResponse<>(false, message, errorCode);
        response.setData(data);
        return response;
    }

    public static <T> ApiResponse<T> error(String message, String errorCode) {
        return new ApiResponse<>(false, message, errorCode);
    }

    public static <T> ApiResponse<T> error(String message) {
        return new ApiResponse<>(false, message, "UNKNOWN_ERROR");
    }

}